<?php

namespace app\index\controller;


class Saler extends Base
{
    public function index()
    {
        $list=db('saler')->paginate(25);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function edit()
    {
        if(request()->isPost()){
            $data=array();
            $data['username']=input('username');
            $data['phone']=input('phone');
            $data['status']=input('status');
            $res=db('saler')->where('id',input('id'))->update($data);
            if($res){
                $this->assign('str',"修改成功");
                return  $this->fetch("index/ok");
            }else{
                $this->assign('str',"修改失败");
                return  $this->fetch("index/fail");
            }
        }else{
            $id=input('id');
            $list=db('saler')->where('id',$id)->find();
            $this->assign('list',$list);
            return $this->fetch();
        }

    }

    public function setStatus(){
        $id=input('id');
        $status=input('status');
        $res=db('saler')->where('id',$id)->setField('status',$status);
        if($res){
            return json(['code'=>1]);
        }else{
            return json(['code'=>0]);
        }

    }
    public function del()
    {
        $id=input('id');
        $res=db('saler')->where('id',$id)->delete();
        if($res){
            return json(['code'=>1]);
        }else{
            return json(['code'=>0]);
        }
    }

    //成绩导入
    public function inputExcel(){
        set_time_limit(0);
        $file = request()->file('file');

        $info=$file->getInfo();
        $pathinfo=pathinfo($info['name']);
//        halt($pathinfo);
        $extension=strtolower($pathinfo['extension']);
        if($extension!="xls" && $extension!="xlsx"){
            return json(['code'=>2,'msg'=>"文件格式不正确"]);
        }
        if($extension=="xlsx"){
            $type="Excel2007";
        }else{
            $type="Excel5";
        }
        $filepath='./Excel/';
        $res=$file->move($filepath);
        $filepath.=str_replace("\\","/",$res->getSaveName());
        require_once "../extend/phpexcel/PHPExcel.php";
        include "../extend/phpexcel/PHPExcel/Writer/".$type.".php";
        require_once "../extend/phpexcel/PHPExcel/IOFactory.php";
        require_once "../extend/phpexcel/PHPExcel/Shared/Date.php";
        $objReader=\PHPExcel_IOFactory::createReader($type);
        $excel=$objReader->load($filepath,"utf-8");
        $sheet=$excel->getSheet(0);
        $rows=$sheet->getHighestRow();
        $cols=$sheet->getHighestColumn();
        $file=['username','phone'];
        $all=0;
        $end=0;
        $str="";
        $filename="";
        for ($currentRow = 2; $currentRow <= $rows; $currentRow++) {
            $tmp=array();
            $i=0;
            for ($currentColumn = 'A'; $currentColumn <= $cols; $currentColumn++) {
                $val=$sheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();
//                dump($val);
                if($val){
                    $tmp[$file[$i]]=$val;
                }
                $i++;
            }
            $all++;
//            dump($tmp);
            //验证插入数据库
            $list=db('saler')
                ->where('username',$tmp['username'])
                ->where('phone',$tmp['phone'])
                ->find();
            if(!$list){
                $salerData=array();
                $salerData['username']=$tmp['username'];
                $salerData['phone']=$tmp['phone'];
                $salerData['add_time']=date('Y-m-d H:i:s');
                db('saler')->insert($salerData);
                $end++;
            }else{
                $str.=$tmp['username']."导入失败\n";
            }
        }
        if($str){
            $filename='./Excel/'.date("Ymd")."/导入销售失败日志.log";
            $dir='./Excel/'.date("Ymd");
            if(!is_dir($dir)){
                mkdir($dir);
                chmod($dir,777);
            }

            file_put_contents($filename,$str);
        }
        return json(['code'=>1,'msg'=>"成功","data"=>['all'=>$all,'end'=>$end,"filename"=>ltrim($filename,".")]]);
    }
}